package com.ruoyi.web.controller.community;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.community.domain.*;
import com.ruoyi.community.mapper.EcNavigateMapper;
import com.ruoyi.community.mapper.EcRestaurantApplyMapper;
import com.ruoyi.community.mapper.EcRestaurantsMapper;
import com.ruoyi.community.service.RestaurantService;
import com.ruoyi.community.service.ec.IEcRestaurantsService;
import com.ruoyi.community.utils.MessageUtil;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

@RestController
@RequestMapping("/community/ec")
public class RestaurantController extends BaseController {
//    @Autowired
//    RestaurantService restaurantService;

    @Autowired
    EcRestaurantsMapper ecRestaurantsMapper;

    @Autowired
    EcNavigateMapper ecNavigateMapper;

    @Autowired
    private IEcRestaurantsService ecRestaurantsService;

    @Autowired
    EcRestaurantApplyMapper ecRestaurantApplyMapper;

    @GetMapping("/navigates")
    public List<EcNavigate> getNavigates() {
        return ecNavigateMapper.selectEcNavigateList(new EcNavigate());
    }

    @GetMapping("/restaurants")
    public TableDataInfo getRestaurants(EcRestaurants ecRestaurants) {
//        List<EcRestaurants> list = ecRestaurantsMapper.selectEcRestaurantsListByCommunity(SecurityUtils.getCommunityId());
        List<SysRole> roles = SecurityUtils.getLoginUser().getUser().getRoles();
        HashSet<String> roleSet = new HashSet<>();
        for (int i = 0; i < roles.size(); i++) {
            roleSet.add(roles.get(i).getRoleKey());
        }
        if (roleSet.contains("admin")) {
            startPage();
//            List<EcRestaurants> list = ecRestaurantsMapper.selectEcRestaurantsListByCommunity(SecurityUtils.getCommunityId());
            ecRestaurants.setCommunityId(SecurityUtils.getCommunityId());
            System.out.println(ecRestaurants.toString());
            List<EcRestaurants> list = ecRestaurantsMapper.selectEcRestaurantsList(ecRestaurants);
            return getDataTable(list);
        }
        else {
            startPage();
            ecRestaurants.setCommunityId(SecurityUtils.getCommunityId());
            System.out.println(ecRestaurants.toString());
            List<EcRestaurants> list = ecRestaurantsMapper.selectEcRestaurantsListByCommunity(SecurityUtils.getCommunityId());
            List<EcRestaurantApply> applyList = ecRestaurantApplyMapper.selectByUserId(SecurityUtils.getUserID().intValue());
            HashSet<Long> ridSet = new HashSet<>();
            for (int i = 0; i < applyList.size(); i++) {
                ridSet.add(applyList.get(i).getRestaurantId());
            }
            List<EcRestaurants> newList = new ArrayList<>();
            for (int i = 0; i < list.size(); i++) {
                if (ridSet.contains(list.get(i).getRestaurantId())) {
                    newList.add(list.get(i));
                }
            }
            TableDataInfo temp = getDataTable(newList);
            temp.setTotal(newList.size());
            return temp;
        }
    }

//    @GetMapping("/restaurants/{communityId}")
//    public List<EcRestaurants> getRestaurantsByCommunityId(@PathVariable("communityId") Integer communityId){
//        return ecRestaurantsMapper.selectEcRestaurantsListByCommunity(communityId);
//    }

    @PostMapping("/restaurant")
    public AjaxResult addRestaurant(@RequestBody EcRestaurants ecRestaurants) {
        ecRestaurants.setCommunityId(SecurityUtils.getCommunityId());
        int r1 = ecRestaurantsMapper.insert(ecRestaurants);
        EcRestaurantApply ecRestaurantApply = new EcRestaurantApply();
        ecRestaurantApply.setRestaurantId(ecRestaurants.getRestaurantId());
        ecRestaurantApply.setUserId(SecurityUtils.getUserID().intValue());
        int r2 = ecRestaurantApplyMapper.insert(ecRestaurantApply);
        AjaxResult ajaxResult;
        if (r1 > 0 && r2 > 0) {
            ajaxResult = AjaxResult.success();
        }
        else {
            ajaxResult = AjaxResult.error();
        }
        return ajaxResult;
    }

    @PutMapping("/restaurantState")
    public AjaxResult updateRestaurantState(@RequestBody EcRestaurants ecRestaurants) {
        Remind remind = new Remind();
        remind.setSenderId(SecurityUtils.getUserID());
        remind.setSenderName(SecurityUtils.getUsername());
        if (ecRestaurants.getCheckState() == 1) {
            remind.setContent("店铺审核通过");
        }
        else {
            remind.setContent("店铺下架");
        }
        remind.setContentType("店铺审核消息");
        remind.setRecipientId(ecRestaurantApplyMapper.selectByRestaurantId(ecRestaurants.getRestaurantId()).getUserId().longValue());
        remind.setUrl("/e-commerce/shops");
        remind.setSenderAction(RemindAction.DIANOUZHUANGTAI);
        MessageUtil.addRemind(remind);
        return toAjax(ecRestaurantsMapper.updateByPrimaryKey(ecRestaurants));
    }

    @PutMapping("/restaurant")
    public AjaxResult updateRestaurant(@RequestBody EcRestaurants ecRestaurants) {
        return toAjax(ecRestaurantsMapper.updateByPrimaryKey(ecRestaurants));
    }

    @DeleteMapping("/restaurant/{id}")
    public AjaxResult removeRestaurant(@PathVariable("id") Long id)
    {
        return toAjax(ecRestaurantsMapper.deleteByPrimaryKey(id));
    }

}
